package com.example.thridhero.remember;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    private TextView userNameView;
    private TextView passWordView;
    private CheckBox checkBoxView;
    private Button logonBtn ;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userNameView = (TextView) findViewById(R.id.user_name);
        passWordView = (TextView) findViewById(R.id.password);
        checkBoxView = (CheckBox) findViewById(R.id.remember_code);
        logonBtn = (Button) findViewById(R.id.login);




        final SharedPreferences sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
        Boolean flag = sharedPreferences.getBoolean("remember",false);

        if(flag){
            String userName = sharedPreferences.getString("userName","");
            String password = sharedPreferences.getString("passWord","");
            userNameView.setText(userName);
            passWordView.setText(password);
            checkBoxView.setChecked(flag);
        }


        logonBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Boolean flag = checkBoxView.isChecked();
                SharedPreferences.Editor editor = sharedPreferences.edit();
                if(flag){
                    editor.putString("userName",userNameView.getText().toString());
                    editor.putString("passWord",passWordView.getText().toString());
                    editor.putBoolean("remember",checkBoxView.isChecked());
                    editor.apply();
                }else{
                    editor.clear();
                }

                Intent intent = new Intent(MainActivity.this,IndexActivity.class);
                startActivity(intent);
            }
        });


    }
}
